const Controller = require('egg').Controller;

class HomeController extends Controller {

    /// 获取资讯列表数据
    async getPeo() {
        const { ctx, app } = this
        const data = await ctx.service.people.getPeo()
        if (typeof data == 'string') {
            ctx.body = {
                code: -1,
                message: data
            }
        } else {
            ctx.body = {
                code: 1,
                data
            }
        }
    }

    /// 添加资讯数据
    async addPeo() {
        const { ctx, app } = this
        const data = await ctx.service.people.addPeo()
        if (data.affectedRows === 1) {
            ctx.body = {
                code: 1,
                message: '添加成功'
            }
        } else {
            ctx.body = {
                code: -1,
                message: '添加失败'
            }
        }
    }

    /// 编辑资讯数据
    async editPeo() {
        const { ctx, app } = this
        const data = await ctx.service.people.editPeo()
        if (data.affectedRows === 1) {
            ctx.body = {
                code: 1,
                message: '编辑成功'
            }
        } else {
            ctx.body = {
                code: -1,
                message: '编辑失败'
            }
        }
    }

    /// 删除资讯数据
    async delPeo() {
        const { ctx, app } = this;
        const data = await ctx.service.people.delPeo()
        if (data.affectedRows === 1) {
            ctx.body = {
                code: 1,
                message: '删除成功'
            }
        } else {
            ctx.body = {
                code: -1,
                message: '删除失败'
            }
        }
    }

    /// 判断经纪人列表数据
    async switchBroker() {
        const { ctx, app } = this
        const data = await ctx.service.people.switchBroker()
        if (typeof data == 'string') {
          ctx.body = {
            code: -1,
            message: data
          }
        } else {
          ctx.body = {
            code: 1,
            data
          }
        }
      }
}

module.exports = HomeController;